home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: MegaDisc / MegaDisc 38 (1994-02)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).zip / MegaDisc 38 (1994-02)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2).adf / Programming / Introduction_To_C / Chapter2 / Chapter2
Text File  |  1994-02-09  |  4KB  |  100 lines

  1.  
  2.                              Chapter Two
  3.  
  4.                           Fundamentals of C
  5.  
  6.                             by Jason Lowe
  7.  
  8.  
  9.  
  10.   38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38
  11.  
  12.  
  13. How a C program is created.
  14.    When you want to program in C you need two things, a C compiler and
  15.   a text editor.  With the text editor you create a text file
  16.   containing a list of C instructions (called the C source) and save
  17.   it to disk.  You then use the C compiler to take this C source from
  18.   disk and create the final runable file (called the executable).
  19.  
  20.    When you create the C source and save it to disk its filename must
  21.   end with '.c'.  Then when you use the C compiler to create the
  22.   executable the filename of the executable will be the same as the C
  23.   source except it will have the '.c' chopped off.  For example,
  24.  
  25.                            c source       executable
  26.                            --------       ----------
  27.                           mysource.c  -->  mysource
  28.                            bananas.c  -->  bananas
  29.                               game.c  -->  game
  30.                             pacman.c  -->  pacman
  31.  
  32.    When you call your C compiler your C source code will go through
  33.   three stages before it becomes an executable.  Firstly your C source
  34.   will be compiled into assembler source by the compiler.  Then the
  35.   assembler source will be assembled into object code by the
  36.   assembler.  Finally, the linker takes this object code, combines it
  37.   with the necessary libaries and out pops the executable.  Here's a
  38.   visual representation of this,
  39.  
  40.               +----------+     +-----------+     +--------+
  41.    source --> | Compiler | --> | Assembler | --> | Linker | --> executable
  42.               +----------+     +-----------+     +--------+
  43.  
  44.    If the compiler finds any errors in your C source you must correct
  45.   these errors and then run it through the compiler again.  Once there
  46.   are no errors in your C source you can then call the assembler and
  47.   the linker to create the executable.  Sometimes you will come across
  48.   errors when linking but this will be discussed later on.
  49.  
  50.  
  51. How to use your C compiler
  52.    A C compiler is usually run through the cli with the name of your C
  53.   source as one of the arguments.  The other arguments are parameters
  54.   to how your C program is to perform.
  55.  
  56.    If you are using the C compiler found on fish 508 you must use four
  57.   seprerate programs in order to convert your C source into an
  58.   executable.  These programs are hcc, top, a68k and blink.  In this C
  59.   compiler hcc is the compiler, both top and a68k are the assembler
  60.   and blink is the linker.
  61.  
  62.    Let's suppose you want to compile the C program 't.c'.  Here's how
  63.   you do it,
  64.  
  65. HCC -L t.c t.t
  66.  
  67.    If hcc found no errors in 't.c' you are ready the create the
  68.   executable.  Here's how you'd do this,
  69.  
  70. HCC -L t.c t.t
  71. TOP t.c t.s
  72. a68k t.s t.o
  73. BLINK FROM lib:hcc.o+t.o LIB lib:hcc.lib+lib:stubs+lib:amiga.lib TO t
  74.  
  75.    As you can see this is quite a bit to type in each and everytime
  76.   you want to create the executable.  It's a good idea to save this as
  77.   a text file and then call the amigados command 'execute'.  For
  78.   example, if you saved the above as 'link' you would simply call,
  79.  
  80. execute link
  81.  
  82.     and your executable would be created.
  83.  
  84.    If you are using the C compiler from the public domain libraries of
  85.   Megadisc (UT 337 A,B) you can either follow the above method or use
  86.   the two small programs 'compile' and 'compile&link'.  As you might
  87.   guess the program 'compile' compiles your C source and
  88.   'compile&link' creates the executable.  For example,
  89.  
  90. compile myprogram.c        <-- Compile the C source 'myprogram.c'
  91. compile&link myprogram.c   <-- Create the exectuable from 'myprogram.c'
  92.  
  93.    If you are using a different compiler from fish 508 consult your
  94.   own documentation to find out how to compile a program and how to
  95.   create the executable.
  96.  
  97.  
  98.   38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38 *^* 38
  99.  
  100.